import conf_zb_app as conf
#from openpyxl import Workbook

a = conf.conf_list()


def to_text():
    # 纯文本输出
    lines = []
    for key, value in a.items():
        name, ip, user = key, value[0][0], value[0][1]

        cmd = value[1]["sys_command"]
        if len(value) > 2:
            for i in range(2, len(value)):
                cmd = cmd + '\t' + value[i]["sys_command"]
        # 纯文本输出
        #lines.append(f'{name}\t{ip}\t{user}\t{cmd}\n')
        lines.append(f'{ip}\n')

    # 纯文本输出
    with open('result.txt', 'w') as f:
        f.writelines(lines)
    print('success')


def to_excel():
    wb = Workbook()
    ws = wb.active
    ws.title = "Sheet1"
    ws.append(['名称', 'IP', '用户名', '命令'])
    for key, value in a.items():
        name, ip, user = key, value[0][0], value[0][1]

        cmd = [value[1]["sys_command"]]
        if len(value) > 2:
            for i in range(2, len(value)):
                cmd.append(value[i]["sys_command"])
        ws.append([name, ip, user] + cmd)

    wb.save('result.xlsx')
    print('success')


to_text()

